//
//  EFMyDeliveryAddressViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/7.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyDeliveryAddressViewCell.h"
#import "EFMyDeliveryAddressViewItem.h"

@interface EFMyDeliveryAddressViewCell()

@property(strong,nonatomic) UIView* headerView;
@property(strong,nonatomic) UILabel* nameLabel;
@property(strong,nonatomic) UILabel* addressLabel;
@property(strong,nonatomic) UIButton* selectButton;
@property(strong,nonatomic) UILabel* markLabel;
@property(strong,nonatomic) UILabel* defaultLabel;
@property(strong,nonatomic) UIView* lineView;
@property(strong,nonatomic) UIImageView* editIv;
@property(strong,nonatomic) UILabel* editLabel;
@property(strong,nonatomic) UIImageView* removeIv;
@property(strong,nonatomic) UILabel* removeLabel;



@end

@implementation EFMyDeliveryAddressViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)initView{
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.addressLabel];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.selectButton];
    [self.contentView addSubview:self.markLabel];
    [self.contentView addSubview:self.defaultLabel];
    [self.contentView addSubview:self.editIv];
    [self.contentView addSubview:self.editLabel];
    [self.contentView addSubview:self.removeIv];
    [self.contentView addSubview:self.removeLabel];
}


-(void)setObject:(EFMyDeliveryAddressViewItem *)item{
    NSString* strNamePhone = NSStringF(@"%@  %@",item.name,item.phone);
    [self.nameLabel setText:strNamePhone];
    
    [self.addressLabel setText:item.address];
    if(item.tag != nil  && ![item.tag isEqual:@"无"]){
        [self.markLabel setText: item.tag];
    }
    self.selectButton.selected = item.selected;
    if(self.selectButton.selected ){
        self.markLabel.textColor = RGBCOLOR(0x69BA45);
        [self.defaultLabel setText:@"默认"];
    }else{
        self.markLabel.textColor = kBlack999;
        [self.defaultLabel setText:@""];
    }
    
    self.id = item.id;
    if(item.hideEdit){
        self.headerView.hidden = true;
        self.selectButton.hidden = true;
        self.markLabel.hidden = true;
        self.editIv.hidden = true;
        self.editLabel.hidden = true;
        self.removeIv.hidden = true;
        self.removeLabel.hidden = true;
        
        
        self.nameLabel.left = 21;
        self.nameLabel.top = 19;
        self.nameLabel.width = kScreenWidth;
        self.nameLabel.height = 17;
        
        self.addressLabel.left = 21;
        self.addressLabel.top = self.nameLabel.bottom + 5;
        self.addressLabel.width = kScreenWidth;
        self.addressLabel.height = 17;
        
        self.lineView.left = 20;
        self.lineView.top = self.addressLabel.bottom + 7;
    }else{
        
        self.nameLabel.left = 20;
        self.nameLabel.top = self.headerView.bottom + 20;
        
        self.addressLabel.top = self.nameLabel.bottom + 3;
        self.addressLabel.left = 20;
        
        self.lineView.left = 20;
        self.lineView.top = self.addressLabel.bottom + 7;
        
        self.selectButton.left = 20;
        self.selectButton.top = self.lineView.bottom + 19;
        
        if(item.tag != nil  && ![item.tag isEqualToString:@""] && ![item.tag isEqual:@"无"]){
            self.markLabel.left = self.selectButton.right + 5;
            self.markLabel.top = self.lineView.bottom + 22;
            
            self.defaultLabel.left = self.markLabel.right + 5;
            self.defaultLabel.top = self.lineView.bottom + 22;
        }else{
            self.defaultLabel.left = self.selectButton.right + 5;
            self.defaultLabel.top = self.lineView.bottom + 22;
        }
    
        self.removeLabel.left = kScreenWidth - self.removeLabel.width - 20;
        self.removeLabel.top = self.lineView.bottom + 22;
        
        self.removeIv.left = self.removeLabel.left - self.removeIv.width - 3;
        self.removeIv.top = self.lineView.bottom + 19;
        
        self.editLabel.left = self.removeIv.left - self.editLabel.width - 13;
        self.editLabel.top = self.lineView.bottom + 22;
        
        self.editIv.left = self.editLabel.left - self.editIv.width - 3;
        self.editIv.top = self.lineView.bottom + 19;
    }
    
}



-(void)onClickSelectBtn{
    self.selectButton.selected = !self.selectButton.selected;
    if(self.selectButton.selected ){
        self.markLabel.textColor = RGBCOLOR(0x69BA45);
        [self.defaultLabel setText:@"默认"];
    }else{
        self.markLabel.textColor = kBlack999;
        [self.defaultLabel setText:@""];
    }
    if(self.selectblock){
        self.selectblock(self.id,self.selectButton.selected);
    }
}

-(UIView*)headerView{
    if(_headerView == NULL){
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth  , 10)];
        _headerView.backgroundColor = RGBCOLOR(0xF4F4F4);
    }
    return _headerView;
}

-(UILabel*)nameLabel{
    if(_nameLabel == NULL){
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 17)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor =kBlack333;
    }
    return _nameLabel;
}


-(UILabel*)addressLabel{
    if(_addressLabel == NULL){
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 34)];
        _addressLabel.font = [UIFont systemFontOfSize:15];
        _addressLabel.textColor = kBlack333;
        _addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _addressLabel;
}

-(UIView*)lineView{
    if(_lineView == NULL){
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _lineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _lineView;
}

- (UIButton *)selectButton{
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22.f, 22.f)];
        [_selectButton setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_selectButton setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_selectButton setImage:IMG(@"cartlist_failure_icon") forState:UIControlStateDisabled];
        [_selectButton addTarget:self action:@selector(onClickSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

-(UILabel*)markLabel{
    if(_markLabel == NULL){
        _markLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 17)];
        _markLabel.font = [UIFont systemFontOfSize:13];
        _markLabel.textColor =kBlack999;
    }
    return _markLabel;
}

-(UILabel*)defaultLabel{
    if(_defaultLabel == NULL){
        _defaultLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 17)];
        _defaultLabel.font = [UIFont systemFontOfSize:13];
        _defaultLabel.textColor = RGBCOLOR(0x69BA45);
    }
    return _defaultLabel;
}

-(UIImageView*)editIv{
    if(_editIv == NULL){
        _editIv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
        [_editIv setImage:IMG(@"address_edit_icon")];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickEdit)];
        [_editIv addGestureRecognizer:tap];
        _editIv.userInteractionEnabled = YES;
    }
    return _editIv;
}

-(UILabel*)editLabel{
    if(_editLabel == NULL){
        _editLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 17)];
        _editLabel.font = [UIFont systemFontOfSize:13];
        _editLabel.textColor =kBlack999;
        _editLabel.text = @"编辑";
        [_editLabel sizeToFit];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickEdit)];
        [_editLabel addGestureRecognizer:tap];
        _editLabel.userInteractionEnabled = YES;
    }
    return _editLabel;
}


-(UIImageView*)removeIv{
    if(_removeIv == NULL){
        _removeIv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
        [_removeIv setImage:IMG(@"address__edit_icon")];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRemove)];
        [_removeIv addGestureRecognizer:tap];
        _removeIv.userInteractionEnabled = YES;
    }
    return _removeIv;
}


-(UILabel*)removeLabel{
    if(_removeLabel == NULL){
        _removeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 17)];
        _removeLabel.font = [UIFont systemFontOfSize:13];
        _removeLabel.textColor =kBlack999;
        _removeLabel.text = @"删除";
        [_removeLabel sizeToFit];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRemove)];
        [_removeLabel addGestureRecognizer:tap];
        _removeLabel.userInteractionEnabled = YES;
    }
    return _removeLabel;
}

-(void)onClickRemove{
    if (self.deleteblock != nil) {
        self.deleteblock();
    }
}

-(void)onClickEdit{
    if (self.editblock != nil) {
        self.editblock();
    }
}


@end
